package com.ruoyi.sbxm.bjgh.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sbxm.bjgh.mapper.BjghMapper;
import com.ruoyi.sbxm.bjgh.service.IBjghService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.sbxm.bjgh.domain.BjghFj;
import com.ruoyi.sbxm.bjgh.domain.Bjgh;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 报奖规划Service业务层处理
 *
 * @author huacai
 * @date 2024-11-12
 */
@Service
public class BjghServiceImpl implements IBjghService
{
    @Resource
    private BjghMapper bjghMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询报奖规划
     *
     * @param bjghId 报奖规划主键
     * @return 报奖规划
     */
    @Override
    public Bjgh selectBjghByBjghId(String bjghId)
    {
        return bjghMapper.selectBjghByBjghId(bjghId);
    }

    /**
     * 查询报奖规划列表
     *
     * @param bjgh 报奖规划
     * @return 报奖规划
     */
    @Override
    public List<Bjgh> selectBjghList(Bjgh bjgh)
    {
        return bjghMapper.selectBjghList(bjgh);
    }

    /**
     * 新增报奖规划
     *
     * @param bjgh 报奖规划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBjgh(Bjgh bjgh)
    {
        bjgh.setCreateTime(DateUtils.getNowDate());
        int rows = bjghMapper.insertBjgh(bjgh);
        insertBjghFj(bjgh);
        return rows;
    }

    /**
     * 批量新增报奖规划
     *
     * @param bjghs 报奖规划List
     * @return 结果
     */
    @Override
    public int batchInsertBjgh(List<Bjgh> bjghs)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(bjghs)) {
            try {
                for (int i = 0; i < bjghs.size(); i++) {
                    int row = bjghMapper.insertBjgh(bjghs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == bjghs.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改报奖规划
     *
     * @param bjgh 报奖规划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBjgh(Bjgh bjgh)
    {
        bjgh.setUpdateTime(DateUtils.getNowDate());
//        bjghMapper.deleteBjghFjByBjghId(bjgh.getBjghId());
        insertBjghFj(bjgh);
        return bjghMapper.updateBjgh(bjgh);
    }

    /**
     * 批量删除报奖规划
     *
     * @param bjghIds 需要删除的报奖规划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBjghByBjghIds(String[] bjghIds)
    {
        bjghMapper.deleteBjghFjByBjghIds(bjghIds);
        return bjghMapper.deleteBjghByBjghIds(bjghIds);
    }

    /**
     * 删除报奖规划信息
     *
     * @param bjghId 报奖规划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBjghByBjghId(String bjghId)
    {
        bjghMapper.deleteBjghFjByBjghId(bjghId);
        return bjghMapper.deleteBjghByBjghId(bjghId);
    }

    /**
     * 新增附件
     * @param bjghFj
     * @return
     */
    @Override
    public int insertBjghFjByBjghId(BjghFj bjghFj) {
        return bjghMapper.insertBjghFjByBjghId(bjghFj);
    }

    /**
     * 根据ID查询附件列表
     * @param bjghFj
     * @return
     */
    @Override
    public List<BjghFj> selectBjghFjList(BjghFj bjghFj) {
        return bjghMapper.selectBjghFjList(bjghFj);
    }

    /**
     * 删除报奖规划附件
     * @param fjIds
     * @return
     */
    @Override
    public int deleteBjghFjByFjIds(Integer[] fjIds) {
        return bjghMapper.deleteBjghFjByFjIds(fjIds);
    }

    /**
     * 新增报奖规划附件信息
     *
     * @param bjgh 报奖规划对象
     */
    public void insertBjghFj(Bjgh bjgh)
    {
        List<BjghFj> bjghFjList = bjgh.getBjghFjList();
        String bjghId = bjgh.getBjghId();
        if (StringUtils.isNotNull(bjghFjList))
        {
            List<BjghFj> list = new ArrayList<BjghFj>();
            for (BjghFj bjghFj : bjghFjList)
            {
                bjghFj.setBjghId(bjghId);
                list.add(bjghFj);
            }
//            if (list.size() > 0) {
//                bjghMapper.batchBjghFj(list);
//            }
        }
    }
}
